<template>
  <el-dialog
     v-drag
    :title="!dataForm.imageId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :inline="true" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="图片id" prop="imageId">
      <el-input size="mini"  v-model="dataForm.imageId" placeholder="图片id" clearable></el-input>
    </el-form-item>
                                                                                    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="visible = false">取消</el-button>
      <el-button size="mini" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          imageId: 0,
          productId: '',
          name: '',
          url: '',
          uid: '',
          suffix: '',
          type: '',
          size: '',
          isDefault: '',
          storeId: '',
          storeName: '',
          storeCode: '',
          createTime: '',
          updateTime: '',
          createUsername: '',
          createUserId: '',
          updateUsername: '',
          updateUserId: '',
          status: '',
          isDeleted: '',
          remark: ''
        },
        dataRule: {
          productId: [
            { required: true, message: '产品id不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '图片名称不能为空', trigger: 'blur' }
          ],
          url: [
            { required: true, message: '图片路径不能为空', trigger: 'blur' }
          ],
          uid: [
            { required: true, message: '图片uid不能为空', trigger: 'blur' }
          ],
          suffix: [
            { required: true, message: '后缀名不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '类型：1、普通图片，2、logo，3、视频不能为空', trigger: 'blur' }
          ],
          size: [
            { required: true, message: '图片大小不能为空', trigger: 'blur' }
          ],
          isDefault: [
            { required: true, message: '1: 是默认图片不能为空', trigger: 'blur' }
          ],
          storeId: [
            { required: true, message: '门店id不能为空', trigger: 'blur' }
          ],
          storeName: [
            { required: true, message: '门店名称不能为空', trigger: 'blur' }
          ],
          storeCode: [
            { required: true, message: '门店编码不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          createUsername: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          createUserId: [
            { required: true, message: '创建人id不能为空', trigger: 'blur' }
          ],
          updateUsername: [
            { required: true, message: '修改人不能为空', trigger: 'blur' }
          ],
          updateUserId: [
            { required: true, message: '修改人id不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '是否可用 1:是不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '是否删除 1:是不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.imageId = id || 0
        this.visible = true
        this.$nextTick(async () => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.imageId) {
            const res = await this.$http({
              url: `/base/basestoreimage/info/${this.dataForm.imageId}`,
              method: 'get',
              params: this.$http.adornParams()
            })
                let data = res.data
                if (data && data.code === 0) {
                    this.dataForm = data.baseStoreImage
                }
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate(async (valid) => {
          if (valid) {
            const res = await this.$http({
              url:`/base/basestoreimage/${!this.dataForm.imageId ? 'save' : 'update'}`,
              method: 'post',
             data: this.$http.adornData( this.dataForm)
            })
            let data = res.data
            if (data && data.code === 0) {
                this.$message({
                        message: '操作成功',
                        type: 'success',
                        duration: 1500,
                        onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                }
            })
            } else {
                this.$message.error(data.msg)
            }
          }
        })
      }
    }
  }
</script>
